package com.sumainfor.pc.gmio.controller.MktFeign;

import com.google.common.collect.Maps;
import com.sumainfor.common.exception.TCException;
import com.sumainfor.common.util.CustomMap;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.pc.gmio.dao.MktFeign.GmfProviderMapper;
import com.sumainfor.pc.gmio.feign.BasePcMktFeign;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author Eddy·Chen
 * @Email 835033913@qq.com
 * @Create 2019-05-20 13:32
 * 总经办提供者
 */
@RestController
@RequestMapping("/key/provider/gmf")
public class GmfProviderController {

    @Autowired
    private GmfProviderMapper mapper;

    @Autowired
    private BasePcMktFeign mktFeign;

    /**
     * 查询 数据
     *
     * @param params
     * @return
     */
    @PostMapping("/getComFieldData")
    public Map<String, Object> getComFieldData(@RequestBody Map<String, Object> params) {
        List<Integer> miIds = (List<Integer>) params.get("miIds");
        String startTime = (String) params.get("startTime");
        String endTime = (String) params.get("endTime");
        CustomMap data = CustomMap.create();
        //退房间数
        data.put("BackHousingCount", mapper.selectComForBackHousingCount(miIds, startTime, endTime));

        //违约金异常
        data.put("OrderErrorCount", mapper.selectComForOrderError(miIds, startTime, endTime));

        //收款金额
        //1. 查询收款房源条件
        JsonResult<List<String>> result = mktFeign.filterCenterForHousing(CustomMap.create("hIds", mapper.selectComForInHids(startTime, endTime))
                .put("miIds", miIds));
        if (result.isSuccess() == false) {
            throw new TCException("请求异常");
        }
        data.put("InMoney", mapper.selectComForMoneyIn(result.getResult(), startTime, endTime));

        //付款金额
        //1. 查询付款房源条件
        JsonResult<List<String>> result1 = mktFeign.filterCenterForHousing(CustomMap.create("hIds", mapper.selectComForOutHids(startTime, endTime))
                .put("miIds", miIds));
        if (result1.isSuccess() == false) {
            throw new TCException("请求异常");
        }
        data.put("OutMoney", mapper.selectComForMoneyOut(result1.getResult(), startTime, endTime));
        return data;
    }

    /**
     * 投诉类型统计
     *
     * @param year
     * @return
     */
    @PostMapping("/getComplaint")
    public List<Map<String, Object>> getComplaint(String year) {
        return mapper.selectComplaint(year);
    }


    /**
     * 根据房源查询收费标准数据
     *
     * @param params
     * @return
     */
    @PostMapping("/selectRates")
    public JsonResult selectRates(@RequestParam Map<String, Object> params) {
        if (MapUtils.isEmpty(params)) {
            return JsonResult.errorForEmpty();
        }
        return JsonResult.success(mapper.selectRates(params));
    }

}
